package cn.hh.ymcc.web.controller;

import cn.hh.ymcc.dto.CourseDto;
import cn.hh.ymcc.service.ICourseService;
import cn.hh.ymcc.domain.Course;
import cn.hh.ymcc.query.CourseQuery;
import cn.hh.ymcc.result.JSONResult;
import cn.hh.ymcc.result.PageList;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;


    /**
     * 查询秒杀订单结算页详细
     */
    @GetMapping("/online/oneByOrder/{orderNo}")
    public JSONResult oneByOrder(@PathVariable("orderNo") String orderNo) {
//        CourseDto courseDto = courseService.oneByOrder(orderNo);
        return courseService.oneByOrder(orderNo);
    }

    /**
     * 获取订单信息
     * 确认订单页需要把课程数据回显
     * @param courseIds
     * @return
     */
    @GetMapping("/info/{courseIds}")
    public JSONResult info(@PathVariable String courseIds){
        return courseService.info(courseIds);
    }

    /**
     * 获取订单信息
     * 后端调用
     * @param courseIds
     * @return
     */
    @GetMapping("/rpc//info/{courseIds}")
    public JSONResult rpcInfo(@PathVariable String courseIds){
        return courseService.rpcInfo(courseIds);
    }

    /**
     *  查询课程详细
     * @param courseId
     * @return
     */
    @GetMapping(value="/getDetail/{courseId}")
    public JSONResult getDetail(@PathVariable Long courseId){
        return courseService.getDetail(courseId);
    }

    /**
     *  获取视频播放
     * @param mediaId
     * @return
     */
    @GetMapping("/getForUser/{mediaId}")
    public JSONResult getForUser(@PathVariable Long mediaId){
        return JSONResult.success(courseService.getForUser(mediaId));
    }



    /**
     *  课程下架
     * @param courseId
     * @return
     */
    @RequestMapping(value="/offLineCourse/{courseId}")
    public JSONResult offLineCourse(@PathVariable Long courseId){
        courseService.offLineCourse(courseId);
        return JSONResult.success();
    }


    /**
     *  课程发布
     * @param courseId
     * @return
     */
    @PreAuthorize("hasAuthority('course:online')") // 方法授权
    @RequestMapping(value="/onLineCourse/{courseId}")
    public JSONResult onLineCourse(@PathVariable Long courseId ){
        courseService.onLineCourse(courseId);
        return JSONResult.success();
    }


    /**
    * 新增
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult save(@RequestBody CourseDto dto){
        courseService.save(dto);
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        Wrapper<Course> tWrapper = new EntityWrapper<Course>();
        tWrapper.like("name",query.getKeyword());
        page = courseService.selectPage(page,tWrapper); // 有高级查询条件
//        page = courseService.selectPage(page);  没有高级查询条件
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }
}
